package testcase.app;

import java.io.IOException;
import java.net.URLEncoder;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import api.app.Reservation;
import common.Host;
import common.Log;
import common.TestData;


/**
 * 帮忙医APP获取体检预约单Reservation接口测试用例
 * @author lipeng
 *
 */
public class TestReservation
{
	 /**
	  * 帮忙医APP获取体检预约单Reservation接口测试环境数据驱动
	  * @return  测试数据
	  * @throws Exception
	  */
	  @DataProvider(name = "Test_Get")
	  public static String[][] testReservationDataProvider_Get() throws Exception{
		  Host.setup("Test");
		  TestData td = new TestData("app/TestReservation.xml");
		  return td.getParam("Get");
	  }
	 
	 /**
	  * 帮忙医APP体检预约Reservation接口测试环境数据驱动
	  * @return  测试数据
	  * @throws Exception
	  */
	  @DataProvider(name = "Test_Post")
	  public static String[][] testReservationDataProvider_Post() throws Exception{
		  Host.setup("Test");
		  TestData td = new TestData("app/TestReservation.xml");
		  return td.getParam("Post");
	  }
	  
	  /**
	   * 帮忙医APP取消预约Reservation接口测试环境数据驱动
	   * @return 测试数据
	   * @throws ParserConfigurationException
	   * @throws SAXException
	   * @throws IOException
	   */
	  @DataProvider(name="Test_Delete")
	  public static String[][] TestReservationDataProvider_Delete() throws ParserConfigurationException, SAXException, IOException
	  {
		  Host.setup("Test");
		  TestData td = new TestData("app/TestReservation.xml");
		  return td.getParam("Delete");
	  }
	  
      /**
      * 帮忙医APP获取体检预约单Reservation接口测试环境测试用例
      * @param caseName 用例名
      * @param token 调接口需要的token
      * @throws Exception
      */
      @Test (dataProvider="Test_Get",groups={"Test"})
      public void testReservationTestCase_Get(String caseName,String token)throws Exception{
	   
	      //调用接口并将接口返回数据转换成json格式
	      String result = Reservation.get(token);
	      JSONObject JsonResult;
	      if(result==null || result.isEmpty()){
		      JsonResult = null;
	      }
	      else {
	    	  JsonResult  = new JSONObject(result);
	   }
	   
	   //设置检查点
	      switch (caseName) {
	    	  case "Success":
		      Assert.assertEquals(JsonResult.getString("Data").isEmpty(),false);
		      break;
	       default:
		      break;
	      }
      }
      /**
       * 帮忙医APP体检预约Reservation接口测试环境测试用例
       * @param caseName 用例名
       * @param pid 用户pid
       * @param cardNo 卡号
       * @param packageId 套餐号
       * @param attachsJson 
       * @param institutionCode 机构码
       * @param date 日期
       * @param InsName 机构名
       * @param InsAddress 机构地址
       * @param source 预约来源
       * @param Remark 标记
       * @param rTimeSpan 时间段
       * @param rTimeCode 时间段码
       * @param token 用户token
       * @throws JSONException
       * @throws IOException
       */
      @Test(dataProvider="Test_Post",groups="Test")
      public void testReservationTestCase_Post(String caseName,String pid,String cardNo,String packageId,String attachsJson,String institutionCode,String date,String InsName,String InsAddress,String source,String Remark,String rTimeSpan,String rTimeCode,String token) throws JSONException, IOException
      {
    	  //Url编码
    	  attachsJson=URLEncoder.encode(attachsJson,"UTF-8");
    	  InsName=URLEncoder.encode(InsName,"UTF-8");
    	  InsAddress=URLEncoder.encode(InsAddress,"UTF-8");
    	  rTimeSpan=URLEncoder.encode(rTimeSpan,"UTF-8");
    	  //调用接口
    	  String result=Reservation.post(pid, cardNo, packageId, attachsJson, institutionCode, date, InsName, InsAddress, source, Remark, rTimeSpan, rTimeCode, token);
    	  JSONObject jsonResult;
    	  if (result==null||result.isEmpty())
    	  {
    		  jsonResult=null;
    	  }
    	  else
    	  {
    		  jsonResult=new JSONObject(result);
    	  }
    	  switch (caseName)
		{
			case "Success":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "PidNull":
				Assert.assertNull(result);
				break;
			case "CardNoWrong":
				Assert.assertNull(result);
				break;
			case "CardNoNull":
				Assert.assertNull(result);
				break;
			case "PackageIdWrong":
				Assert.assertNull(result);
				break;
			case "PackageIdNull":
				Assert.assertNull(result);
				break;
			case "AttachJsonWrong":
				Assert.assertNull(result);
				break;
			case "AttachJsonNull":
				Assert.assertNull(result);
				break;
			case "InsCodeWrong":
				Assert.assertFalse(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success") );
				break;
			case "InsCodeNull":
				Assert.assertFalse(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success") );
				break;
			case "DateWrong":
				Assert.assertFalse(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success") );
				break;
			case "DateNull":
				Assert.assertNull(result);
				break;
			case "InsNameWrong":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "InsNameNull":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "InsAddWrong":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "InsAddNull":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "SourseWrong":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "SourseNull":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "RemarkWrong":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "RTimeSpanWrong":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "RTimeSpanNull":
				Assert.assertTrue(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success"));
				//取消预约
				jsonResult=new JSONObject(Reservation.delete(jsonResult.getJSONObject("Data").getJSONObject("result").getString("ReserID"), pid, token));
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "RTimeCodeWrong":
				Assert.assertFalse(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success") );
				break;
			case "RTimeCodeNull":
				Assert.assertFalse(jsonResult.getJSONObject("Data").getJSONObject("result").getBoolean("Success") );
				break;
			case "TokenWrong":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
      }
      
      /**
       * 帮忙医APP取消预约接口测试环境测试用例
       * @param caseName 用例名
       * @param userPid 调预约接口用的Pid
       * @param cardNo 调预约接口用的卡号
       * @param packageId 调预约接口用的套餐号
       * @param attachsJson 调预约接口用的attachsJson
       * @param institutionCode 调预约接口用的机构代码
       * @param date 调预约接口用的日期
       * @param InsName 调预约接口用的机构名称
       * @param InsAddress 调预约接口用的机构地址
       * @param source 调预约接口用的source
       * @param Remark 调预约接口用的Remark
       * @param rTimeSpan 调预约接口用的预约时间段
       * @param rTimeCode 调预约接口用的时间段代码
       * @param userToken 调预约接口用的token
       * @param resvNo 调取消预约接口的预约单号
       * @param pid 调取消预约接口的pid
       * @param token 调取消预约接口的token
       * @throws JSONException
       * @throws IOException
       * @throws ParserConfigurationException
       * @throws SAXException
       */
      @Test(dataProvider="Test_Delete",groups="Test")
      public void testReservationTestCase_Delete(String caseName,String userPid,String cardNo,String packageId,String attachsJson,String institutionCode,String date,String InsName,String InsAddress,String source,String Remark,String rTimeSpan,String rTimeCode,String userToken,String resvNo,String pid,String token) throws JSONException, IOException, ParserConfigurationException, SAXException
      {
    	  //Url编码
    	  attachsJson=URLEncoder.encode(attachsJson,"UTF-8");
    	  InsName=URLEncoder.encode(InsName,"UTF-8");
    	  InsAddress=URLEncoder.encode(InsAddress,"UTF-8");
    	  rTimeSpan=URLEncoder.encode(rTimeSpan,"UTF-8");
    	  if (resvNo.equals("get"))
    	  {
    		  String res=Reservation.post(userPid, cardNo, packageId, attachsJson, institutionCode, date, InsName, InsAddress, source, Remark, rTimeSpan, rTimeCode, userToken);
    		  JSONObject jsonObject;
    		  if(res==null || res.isEmpty())
    	      {
    			  Assert.fail("取消预约用例"+caseName+"失败，未能完成预约，请检查日志");
    			  jsonObject = null;
    	      }
    	      else 
    	      {
    	    	  jsonObject  = new JSONObject(res);
    	      }
    		  
    		  resvNo=jsonObject.getJSONObject("Data").getJSONObject("result").getString("ReserID"); 
    	  }
    	 
    	  //调用接口并将接口返回数据转换成json格式
	      String result = Reservation.delete(resvNo, pid, token);
	      JSONObject JsonResult;
	      if(result==null || result.isEmpty())
	      {
		      JsonResult = null;
	      }
	      else 
	      {
	    	  JsonResult  = new JSONObject(result);
	      }
	      
	      switch (caseName)
		{
			case "Success":
				if (!JsonResult.getBoolean("Data"))
				{
					Log.write("接口：取消预约Reservation，用例名："+caseName+";接口返回结果："+JsonResult);
				}
				Assert.assertTrue(JsonResult.getBoolean("Data"));
				break;
			case "ResvNoWrong":
				Assert.assertFalse(JsonResult.getBoolean("Data"));
				break;
			case "ResvNoNull":
				Assert.assertFalse(JsonResult.getBoolean("Data"));
				break;
			case "PidWrong":
				if (!JsonResult.getBoolean("Data"))
				{
					Log.write("接口：取消预约Reservation，用例名："+caseName+";接口返回结果："+JsonResult);
				}
				Assert.assertTrue(JsonResult.getBoolean("Data"));
				break;
			case "PidNull":
				if (!JsonResult.getBoolean("Data"))
				{
					Log.write("接口：取消预约Reservation，用例名："+caseName+";接口返回结果："+JsonResult);
				}
				Assert.assertTrue(JsonResult.getBoolean("Data"));
				break;
			case "TokenWrong":
				Reservation.delete(resvNo, pid, userToken);
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Reservation.delete(resvNo, pid, userToken);
				Assert.assertNull(result);
				break;
			default:
				break;
		}
      }
}
